package com.scau.hwadee.examination.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scau.hwadee.examination.entity.*;
import com.scau.hwadee.examination.mapper.*;
import com.scau.hwadee.examination.service.ProfessionalDeclarationService;
import com.scau.hwadee.examination.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author YHX
 * @date 2019/8/3 14:26
 * description
 */
@Service
public class ProfessionalDeclarationServiceImpl implements ProfessionalDeclarationService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private MajorMapper majorMapper;

    @Autowired
    private CourseoptionalrelationMapper courseoptionalrelationMapper;

    @Autowired
    private ApplicationprofessionalplanadjustmentMapper applicationprofessionalplanadjustmentMapper;

    @Autowired
    private MajorplanadjustcourseMapper majorplanadjustcourseMapper;

    @Autowired
    private MajorrenewapprovalMapper majorrenewapprovalMapper;

    @Autowired
    private MajorexpandapprovalMapper majorexpandapprovalMapper;

    @Autowired
    private NewmajordeclarationMapper newmajordeclarationMapper;

    @Override
    public PageInfo<Major> getAllMajorsOfSchool(int pageNum, int pageSize, String schoolId) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(majorMapper.selectAllMajorsOfSchool(pageNum, pageSize, schoolId));
    }

    @Override
    public int insertApplicationprofessionalplanadjustment(Applicationprofessionalplanadjustment app) {
        try {
            return applicationprofessionalplanadjustmentMapper.insert(app);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public PageInfo<Course> getAllCourseOfMajor(int pageNum, int pageSize, String majorId) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(courseMapper.selectAllCourseOfMajor(pageNum, pageSize, majorId));
    }

    @Override
    public int insertMajorplanadjustcourse(Majorplanadjustcourse majorplanadjustcourse) {
        try {
            return majorplanadjustcourseMapper.insert(majorplanadjustcourse);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public PageInfo<Majorrenewapproval> getAllApprovedMajorRenewApproval(int pageNum, int pageSize, String schoolId) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(majorrenewapprovalMapper.selectAllApprovedMajorrenewapproval(pageNum, pageSize, schoolId));
    }

    @Override
    public int insertMajorRenewApprovalList(JSONArray jsonArray) {
        String param = JsonUtils.ObjectToJSON(jsonArray);
        List<Majorrenewapproval> majorrenewapprovals = JsonUtils.JSONToList(param, Majorrenewapproval.class);
        try {
            return majorrenewapprovalMapper.insertList(majorrenewapprovals);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public PageInfo<Major> listAllMajorExaminerIsNotTheSchoolId(int pageNum, int pageSize, String schoolId) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(majorMapper.selectAllMajorExaminerIsNotTheSchoolId(pageNum, pageSize, schoolId));
    }

    @Override
    public int insertMajorExpandApprovalList(String param) {
        List<Majorexpandapproval> majorexpandapprovals = JsonUtils.JSONToList(param, Majorexpandapproval.class);
        try {
            return majorexpandapprovalMapper.insertList(majorexpandapprovals);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int insertNewProfessionalDeclaration(Newmajordeclaration newmajordeclaration) {
        try {
            return newmajordeclarationMapper.insert(newmajordeclaration);
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public int insertNewMajorCourseOptionalRelation(Courseoptionalrelation courseoptionalrelation) {
        try {
            return courseoptionalrelationMapper.insert(courseoptionalrelation);
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public PageInfo<Applicationprofessionalplanadjustment> getAllApplicationprofessionalplanadjustment(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(applicationprofessionalplanadjustmentMapper.selectAllApplicationprofessionalplanadjustment(pageNum, pageSize));
    }

    @Override
    public int updateApplicationprofessionalplanadjustment(Applicationprofessionalplanadjustment applicationprofessionalplanadjustment) {
        try {
            return applicationprofessionalplanadjustmentMapper.updateByPrimaryKey(applicationprofessionalplanadjustment);
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public PageInfo<Majorrenewapproval> getAllMajorrenewapproval(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(majorrenewapprovalMapper.selectAllApprovedMajorrenewapproval(pageNum, pageSize, null));
    }

    @Override
    public int updateMajorrenewapproval(Majorrenewapproval majorrenewapproval) {
        try {
            return majorrenewapprovalMapper.updateByPrimaryKey(majorrenewapproval);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public PageInfo<Majorexpandapproval> getAllMajorexpandapproval(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(majorexpandapprovalMapper.selectAllMajorexpandapproval(pageNum, pageSize));
    }

    @Override
    public int updateMajorexpandapproval(Majorexpandapproval majorexpandapproval) {
        try {
            return majorexpandapprovalMapper.updateByPrimaryKey(majorexpandapproval);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public PageInfo<Newmajordeclaration> getAllNewMajorDeclaration(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(newmajordeclarationMapper.selectAllNewmajordeclaration(pageNum, pageSize));
    }

    @Override
    public int updateNewMajorDeclaration(Newmajordeclaration newmajordeclaration) {
        try {
            return newmajordeclarationMapper.updateByPrimaryKey(newmajordeclaration);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
